home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / JForth / JTools / Appls / cr2lf.f next >
Encoding:
FORTH Source  |  1988-08-20  |  2.1 KB  |  86 lines

  1. \ Convert CR characters in input file to LF chars in output file.
  2. \
  3. \ This is useful for converting Macintosh text files
  4. \ downloaded from bulletin boards.
  5. \
  6. \ Author: Phil Burk
  7. \ Copyright 1988 Phil Burk
  8. decimal
  9. ANEW TASK-CR2LF
  10.  
  11. VARIABLE IN-FILEID
  12. VARIABLE OUT-FILEID
  13.  
  14. 64 constant C2L_MAX_CHARS
  15. VARIABLE CHAR-BUFFER C2L_MAX_CHARS ALLOT
  16. VARIABLE C2L-COUNT-CHARS
  17. VARIABLE C2L-COUNT-CR
  18.  
  19. 10 constant LF_CHAR
  20. 13 constant CR_CHAR
  21.  
  22. : C2L.HELP  ( -- , print documentation )
  23.     cr ." Syntax:   CR2LF  infile outfile" cr cr
  24.     ." For example, to convert all carriage returns in" cr
  25.     ." a file called 'MacText' to line feeds and save the" cr
  26.     ." result in a file call 'AmigaText', enter:" cr cr
  27.     ."     CR2LF MacText AmigaText" cr
  28.     cr
  29. ;
  30.  
  31. : C2L.OPEN.FILES ( <infile> <outfile> -- error_flag )
  32.     fopen dup in-fileid !
  33.     IF  new fopen  dup out-fileid !
  34.         IF false
  35.         ELSE in-fileid @ fclose
  36.             cr ." Couldn't open OUTPUT file!" cr
  37.             true
  38.         THEN
  39.     ELSE cr ." Couldn't open INPUT file!" cr true
  40.     THEN
  41. ;
  42.  
  43. : C2L.CLOSE.FILES ( -- )
  44.     in-fileid @ fclose
  45.     out-fileid @ fclose
  46. ;
  47.  
  48. : C2L.CONVERT.TEXT ( addr count -- , convert text string CR>LF )
  49.     0 DO
  50.         i over + c@ cr_char =
  51.         IF i over +  lf_char swap c!
  52.             1 c2l-count-cr +!
  53.         THEN
  54.     LOOP drop
  55. ;
  56.  
  57. : C2L.PROCESS.FILE  ( -- , convert entire file )
  58.     0 c2l-count-chars !
  59.     0 c2l-count-cr !
  60.     BEGIN
  61.         in-fileid @ char-buffer c2l_max_chars fread
  62.         dup 0>
  63.         ?terminal not AND
  64.     WHILE
  65.         dup c2l-count-chars +!
  66.         dup char-buffer swap c2l.convert.text
  67.         >r out-fileid @ char-buffer r> fwrite
  68.         0= abort" File write failed!"
  69.     REPEAT drop
  70.     c2l-count-cr @ . ."  CRs converted to LF, "
  71.     c2l-count-chars @ . ."  characters in file." cr
  72. ;
  73.  
  74. : CR2LF  ( <infile> <outfile> -- )
  75.     cr ." CR2LF - Convert CR to LF in files." cr
  76.     ." Written by Phil Burk using JForth V1.3 from" cr
  77.     ." Delta Research, P.O. Box 1051, San Rafael, CA 94901" cr
  78.     ." Hit <CR> to abort." cr
  79.     c2l.open.files 0=
  80.     IF  c2l.process.file
  81.         c2l.close.files
  82.     ELSE c2l.help
  83.     THEN
  84.  
  85. ;
  86.